package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.pet.domain.PetType;
import cn.itsource.pet.mapper.PetTypeMapper;
import cn.itsource.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> queryTree() {
        List<PetType> typeList = petTypeMapper.findAll();
        Map<Long, PetType> map = new HashMap<>();
        for (PetType p:typeList) {
            map.put(p.getId(),p);
        }
        ArrayList<PetType> tree = new ArrayList<>();
        for (PetType p:typeList) {
            if(p.getParentId()==null){
                tree.add(p);
            }else{
                PetType parent = map.get(p.getParentId());
                if(parent!=null){
                    parent.getChildren().add(p);
                }
            }
        }
        return tree;
    }
}
